<?php

class Emms_PmtPlanFlat Extends Emms_PmtPlan
{
    protected function _setPlan()
    {       
        $this->_pmt      = $this->_param['Pmt'];
        $this->_gracePmt = $this->_param['GracePmt'];
        
        $this->_kp = $this->_param['Principal'];
        $this->_kh = 0.01 * $this->_param['Principal'] * $this->_param['HealthInsurancePct'];
        $this->_kl = 0.01 * $this->_param['Principal'] * $this->_param['LoanInsurancePct'];
        $this->_ka = 0.01 * $this->_param['Principal'] * $this->_param['AdministrativeFeePct'];
        $this->_ki = ($this->_pmt * $this->_param['Installments']) - ($this->_kp + $this->_kh + $this->_kl + $this->_ka);
        
        $this->_kt = $this->_kp + $this->_kh + $this->_kl + $this->_ka + $this->_ki;
        
        $this->_kpP = $this->_kp / $this->_kt;
        $this->_khP = $this->_kh / $this->_kt;
        $this->_klP = $this->_kl / $this->_kt;
        $this->_kaP = $this->_ka / $this->_kt;
        $this->_kiP = $this->_ki / $this->_kt;
        
        $this->_balanceKp = $this->_param['Loan'] ? $this->_param['Loan']->d->BalancePrincipal         : $this->_kp;
        $this->_balanceKh = $this->_param['Loan'] ? $this->_param['Loan']->d->BalanceHealthInsurance   : $this->_kh;
        $this->_balanceKl = $this->_param['Loan'] ? $this->_param['Loan']->d->BalanceLoanInsurance     : $this->_kl;
        $this->_balanceKa = $this->_param['Loan'] ? $this->_param['Loan']->d->BalanceAdministrativeFee : $this->_ka;
        $this->_balanceKi = $this->_ki;
        
        //$this->_plan->annualEffectiveRate;
                
        $this->_xpPmt     = $this->_param['Loan'] ? $this->_param['Loan']->d->PaymentMnt          : $this->_pmt;
        $this->_xpPmtDate = $this->_param['Loan'] ? $this->_param['Loan']->d->NextPaymentDate     : $this->_param['FirstPaymentDate'];
        $this->_rFromDate = $this->_param['Loan'] ? $this->_param['Loan']->d->PreviousPaymentDate : $this->_param['PrincipalGraceEndDate'];
        
        $this->_schedule = $this->_getSchedule();                    
    }
    
    protected function _nextPaymentData()
    {       
        $data = array();        
        $balance = $this->_balanceKp + $this->_balanceKh + $this->_balanceKl + $this->_balanceKa + $this->_balanceKi;

        $data['xpPmtDate'] = self::nextPaymentDate($this->_xpPmtDate, $this->_param['PaymentFrequency']);
        $data['rFromDate'] = $this->_xpPmtDate;
        
        $data['hFee']      = round($this->_khP * $this->_pmt, 2);
        $data['lFee']      = round($this->_klP * $this->_pmt, 2);
        $data['aFee']      = round($this->_kaP * $this->_pmt, 2);
        $data['principal'] = round($this->_kpP * $this->_pmt + 0.005, 2);
        $data['interest']  = $this->_pmt - $data['hFee'] - $data['lFee'] - $data['aFee'] - $data['principal'];
        
        $data['balanceKp'] = round($this->_balanceKp - $data['principal'],2);
        $data['balanceKh'] = round($this->_balanceKh - $data['hFee'],2);
        $data['balanceKl'] = round($this->_balanceKl - $data['lFee'],2);
        $data['balanceKa'] = round($this->_balanceKa - $data['aFee'],2);
        $data['balanceKi'] = round($this->_balanceKi - $data['interest'],2);
        
        $data['penalties'] = 0;
        $data['delay'] = 0;
        
        if($this->_param['Loan']) {
            $currentDate = new Zend_Date(date('Y-m-d'),'Y-m-d');
            $pmtDate = new Zend_Date($this->_xpPmtDate,'Y-m-d');  
            if($currentDate >= $pmtDate) {
                $data['delay'] = ceil($currentDate->sub($pmtDate)->toValue()/60/60/24);
                if ($data['delay'] > $this->_param['Loan']->d->DelayGrace) {
                    $data['penalties'] = round($data['delay'] * $this->_pmt * ($this->_param['Loan']->d->DelayPenaltyRate/100) + $this->_param['Loan']->d->DelayPenaltyFixedCharge,2);
                }
            } else {
                $data['delay'] = 0;
            }
        }
        
        $data['pmt']       = $this->_pmt + $data['penalties'];
        $data['xpPmt']     = min($data['pmt'], $balance + $data['interest'] + $data['penalties']);
        
        if ($balance < $this->_pmt) {
          $data['interest']  = $this->_balanceKi;
          $data['hFee']      = $this->_balanceKh;
          $data['lFee']      = $this->_balanceKl;
          $data['aFee']      = $this->_balanceKa;
          $data['principal'] = $this->_balanceKp;
          $data['balanceKp'] = 0;
          $data['balanceKh'] = 0;
          $data['balanceKl'] = 0;
          $data['balanceKa'] = 0;
          $data['pmt']       = $data['interest'] + $data['hFee'] + $data['lFee'] + $data['aFee'] + $data['principal'] + $data['penalties'];
          $data['xpPmt']     = 0;
        }
        return $data;
    }
}